package p2pcdnsim.network;

import java.util.HashMap;

import desmoj.core.simulator.Model;
import desmoj.core.simulator.SimTime;

public class LANBus extends Link {
	
	private HashMap<NetworkAddress,ClientNode> clientNodes;

	public LANBus(Model owner, RouterNode node, int capacity, SimTime delay) {
		super(owner, node, node, capacity, delay);
		this.id = "LAN-Node"+node.getNodeId();
		clientNodes = new HashMap<NetworkAddress, ClientNode>();
	}

	
	public void addClientNode(ClientNode node) {
		clientNodes.put(node.getNetworkLayer().getDefaultAddress(), node);
	}


	@Override
	protected void deliverPacket(Packet p) {
		if(clientNodes.containsKey(p.getDestination())) {
			clientNodes.get(p.getDestination()).receivePacket(p);
		} else {
			//Default gateway
			this.to.receivePacket(p);
		}
	}


	public void removeClientNode(ClientNode node) {
		System.out.println("["+LinkControl.getInstance().currentTime()+"] Disposing client node "+node.getNetworkLayer().getDefaultAddress());
		clientNodes.remove(node.getNetworkLayer().getDefaultAddress());
	}
	
	
}
